package com.eyas.im.gui.swing.index;

import com.eyas.im.common.datasource.DataSourceUtils;
import com.eyas.im.gui.swing.login.Session;
import com.eyas.im.gui.swing.login.dao.IMFriendListInfo;
import com.eyas.im.gui.swing.login.dao.IMUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 好友管理器，根据好友账号进行增删改操作
 */
public class FriendManagerFrame extends JFrame implements ActionListener {
    private final static Logger logger = LoggerFactory.getLogger(FriendManagerFrame.class);
    public final static String ADD_FRIEND_ACTION = "addFriend";
    public final static String REMOVE_FRIEND_ACTION = "removeFriend";
    private final String actionType;
    private final FriendListFrame friendListFrame;
    private JTextField userAccountField;

    public FriendManagerFrame(String actionType,FriendListFrame friendListFrame) {
        this.actionType = actionType;
        this.friendListFrame = friendListFrame;
        if(!actionType.equals(ADD_FRIEND_ACTION)&&!actionType.equals(REMOVE_FRIEND_ACTION))
            logger.warn("不支持的操作:"+actionType);
        else {
            this.initalFrame();
        }
    }

    private void initalFrame() {
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setResizable(false);
        Dimension screenSzie = Toolkit.getDefaultToolkit().getScreenSize();
        double x = screenSzie.getWidth();
        double y = screenSzie.getHeight();

        this.addComponent();
        this.pack();
        Dimension frameSize = this.getSize();
        double fx = frameSize.getWidth();
        double fy = frameSize.getHeight();
        this.setLocation((int) (x / 2 - fx / 2), (int) (y / 2 - fy / 2));
        this.setVisible(true);
    }

    private void addComponent() {
        JPanel mainPanel = new JPanel();
        Font fontBlod20 = new Font("",Font.BOLD,20);

        JLabel userAccountLable = new JLabel("账号:");
        userAccountLable.setFont(fontBlod20);

        userAccountField = new JTextField(20);
        userAccountField.setFont(fontBlod20);

        String buttonName = null;
        switch (this.actionType) {
            case ADD_FRIEND_ACTION:
                buttonName = "新增好友";
                break;
            case REMOVE_FRIEND_ACTION:
                buttonName = "删除好友";
                break;
        }
        JButton actionButton = new JButton(buttonName);
        actionButton.setFont(fontBlod20);
        actionButton.addActionListener(this);


        mainPanel.add(userAccountLable);
        mainPanel.add(this.userAccountField);
        mainPanel.add(actionButton);
        this.add(mainPanel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取录入的用户账户
        String userAccount = this.userAccountField.getText();
        if(userAccount==null||userAccount.equals("")) {
            JOptionPane.showMessageDialog(this,"请录入好友账号");
            return;
        }
        try {
            java.util.List<IMUserInfo> imUserInfos = DataSourceUtils.INSTANCE.query("select * from im_user_info where useraccount=? and validflag='1'",
                    IMUserInfo.class,userAccount);
            if(imUserInfos==null||imUserInfos.size()==0) {
                JOptionPane.showMessageDialog(this,"没有找到好友信息，请检查好友账号是否正确");
                return;
            }
            switch (this.actionType) {
                case ADD_FRIEND_ACTION:
                    java.util.List<IMFriendListInfo> imFriendListInfos = DataSourceUtils.INSTANCE
                            .query("select * from IM_FRIEND_LIST_INFO where userid=? and friendid=? and friendstate='1' ",
                                    IMFriendListInfo.class, Session.CURRENT_USER_INFO.getUserId(),imUserInfos.get(0).getUserId());
                    if(imFriendListInfos!=null&&imUserInfos.size()>0) {
                        JOptionPane.showMessageDialog(this,"已经添加了好友，请不要重复添加");
                    }else {
                        DataSourceUtils.INSTANCE.execute("insert into IM_FRIEND_LIST_INFO(userid,friendid) values(?,?)",
                                Session.CURRENT_USER_INFO.getUserId(),imUserInfos.get(0).getUserId());
                        this.friendListFrame.addFriend(imUserInfos.get(0));
                        JOptionPane.showMessageDialog(this,"成功添加了好友:"+imUserInfos.get(0).getUserAccount());
                        FriendManagerFrame.this.dispose();
                    }
                    break;
                case REMOVE_FRIEND_ACTION:
                    DataSourceUtils.INSTANCE.execute("delete from IM_FRIEND_LIST_INFO where userid=? and friendid=?",
                            Session.CURRENT_USER_INFO.getUserId(),imUserInfos.get(0).getUserId());
                    this.friendListFrame.removeFriend(imUserInfos.get(0));
                    JOptionPane.showMessageDialog(this,"成功删除了好友:"+imUserInfos.get(0).getUserAccount());
                    FriendManagerFrame.this.dispose();
                    break;
            }
        } catch (Exception ex) {
            logger.error("搜索好友时数据库出现异常",ex);
            JOptionPane.showMessageDialog(this,"搜索好友时数据库出现异常");
        }
    }
}
